var app = require('../app'),
    infoId = $("#infoId").val();
var updatacaseid = "",
    updataid = "";
function deal(type, btn){
    btn.attr("disabled","disabled");

    $.ajax({
        url: "/entryrepay/updateStatus",
        type: "get",
        data: {
            id: infoId,
            status: type
        },
        dataType: "json",
        success: function(res){
            if(res.code == 200){
                toastr.success("提交成功，正在去往下一宗案件...");
                setTimeout(function(){
                    var nextUrl = $(".go-next").attr("href");
                    if(nextUrl.indexOf("/") >= 0){
                        window.location.href = nextUrl;
                    }else{
                        toastr.info("没有下一个了，正在去往列表页...");
                        window.location.href = "/entryrepay/index";
                    }
                    $("#confirm").modal("hide");
                },1500);
            }else{
                btn.removeAttr("disabled","disabled");
                toastr.warning(res.message);
            }
        },
        error: function(){
            btn.removeAttr("disabled","disabled");
            toastr.error("网络错误，请稍候再试")
        }
    })
}

$("body")
    .on("click", "#confirmSubmit", function(){
        var type = $("#submitType").val();     // 1:已还 2:未还
        deal(type, $(this));
    })
    .on('click','#updatafileSubmit',function(){
        // loading.showLoading({
        //     type: 1,
        //     tip: '上传中...'
        // })
        var fd = new FormData();
        console.log($("#exampleInputFile")[0].files[0])
        var file_obj = $("#exampleInputFile")[0].files[0];
        if(!file_obj){
            toastr.warning("您没有选择任何文件");
            return;
        }
        fd.append('file', file_obj);
        fd.append('case_id', updatacaseid);
        fd.append('id', updataid);
        $.ajax({
            url: '/entryrepay/uploadRepayEvidence',
            type: 'post',
            data: fd,
            //文件上传时必须配置的参数
            contentType: false,
            processData: false,
            dataType: "json",
            success : function(res){
                uploadingState = 0;
                if(res.code == 200){
                    // loading && loading.hideLoading();
                    toastr.success("文件上传完毕");
                    $("#updatafile").modal("hide");
                }else{
                    // loading && loading.hideLoading();
                    toastr.warning(res.message);
                }
            },
            error : function(){
                // loading && loading.hideLoading();
                toastr.error("服务器异常");
            }
        });
       return false;
    })

    
// 二次确认 的模态框
$('#confirm')
.on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget),
        type = button.data("type"),
        modal = $(this),
        tipsText = $(".fn-button[data-type=" + type + "]").text();

    modal.find('.dialog-tips').html("您要选择 <b>" + tipsText + "</b> 吗？");
    modal.find('#submitType').val(type);
    modal.find('#exampleInputFileId').val(button.attr("data-id"));
});
$('#updatafile')
.on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget),
        type = button.data("type"),
        modal = $(this);
        updatacaseid = button.attr("data-caseid");
        updataid = button.attr("data-id");
});